package com.boesch.electroOffice.utils;

import java.sql.Timestamp;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

public class DateUtils {

	public static LocalDate convertDateToLocalDate(Date date) {
		if(null != date) {
			LocalDate localDate = new DateTime(date).toLocalDate();
			return localDate;
		}
		return null;
	}

	public static LocalDateTime convertDateToLocalDateTime(Date date) {
		LocalDateTime localDateTime = new DateTime(date).toLocalDateTime();
		return localDateTime;
	}

	public static LocalDate convertTimestampToLocalDate(Timestamp timestamp) {
		LocalDate date = new LocalDate(timestamp.getTime());
		return date;
	}

	public static LocalDateTime convertTimestampToLocalDateTime(
			Timestamp timestamp) {
		if (null != timestamp) {
			return new LocalDateTime(timestamp.getTime());
		}
		return new LocalDateTime();
	}
	
	public static Timestamp convertLocalDateToTimestamp(LocalDate date) {
		if(null != date && null != date.toDate()) {
			return new Timestamp(date.toDate().getTime());
		}
		return null;
	}

	public static Timestamp convertLocalDateTimeToTimestamp(LocalDateTime dateTime) {
		if(null != dateTime && null != dateTime.toDate()) {
			return new Timestamp(dateTime.toDate().getTime());
		}
		return null;
	}
	
	public static LocalDate convertStringToLocalDate(String date) {
		LocalDate localDate = LocalDate.parse(date);
		return localDate;
	}

	public static String convertLocalDateToString(LocalDate date) {
		String dateString = "";
		if(null != date) {
			dateString = date.toString("dd.MM.yyyy");
		}
		return dateString;
	}

	public static String convertLocalDateTimeToString(
			LocalDateTime appointmentDate) {
		return appointmentDate.toString("dd.MM.yyyy HH:mm");
	}

	public static Date convertLocalDateTimeToDateTime(
			LocalDateTime appointmentDate) {
		Date date = new Date(appointmentDate.toDate().getTime());
		return date;
	}

	public static java.sql.Date convertLocalDateToDate(LocalDate dateOfBirth) {
		java.sql.Date date = new java.sql.Date(dateOfBirth.toDate().getTime());
		return date;
	}

	public static Timestamp getCurrentTimestamp() {
		DateTime today = new DateTime();
		return new Timestamp(today.getMillis());
	}

	public static String getCurrentDateAsString() {
		LocalDate date = new LocalDate();
		return convertLocalDateToString(date);
	}
	
	public static long getCurrentMillis() {
		LocalDate date = new LocalDate();
		return date.toDateTimeAtCurrentTime().getMillis();
	}

	public static int getCurrentWeek() {
		LocalDate dt = new LocalDate();
		return dt.getWeekOfWeekyear();
	}

	public static int getCurrentMonth() {
		LocalDate dt = new LocalDate();
		int month = dt.getMonthOfYear();
		return month;
	}

	public static int getCurrentYear() {
		LocalDate date = new LocalDate();
		return date.getYear();
	}
	
	public static int getWeekByLocalDate(LocalDate date) {
		return null != date ? date.getWeekOfWeekyear() : 1;
	}

	public static int getMonthByLocalDate(LocalDate date) {
		int month = date.getMonthOfYear();
		return month;
	}

	public static int getYearByLocalDate(LocalDate date) {
		return date.getYear();
	}

	public static LocalDate getDatePlusDays(LocalDate date, int days) {
		LocalDate datePlus = date.plusDays(days);
		return datePlus;
	}

	public static LocalDate getDateMinusDays(LocalDate date, int days) {
		LocalDate dateMinus = date.minusDays(days);
		return dateMinus;
	}
	
	public static Timestamp getDatePlusDays(Timestamp timestamp, int days) {
		LocalDate date = convertTimestampToLocalDate(timestamp);
		LocalDate plusDate = date.plusDays(days);
		return convertLocalDateToTimestamp(plusDate);
	}
	
	public static Timestamp getDateMinusDays(Timestamp timestamp, int days) {
		LocalDate date = convertTimestampToLocalDate(timestamp);
		LocalDate minusDate = date.minusDays(days);
		return convertLocalDateToTimestamp(minusDate);
	}
	
	public static LocalDate getDateOfMondayInCurrentWeek() {
		LocalDate now = new LocalDate();
		LocalDate monday = now.withDayOfWeek(DateTimeConstants.MONDAY);
		return monday;
	}
	
	public static LocalDate getDateOfSundayInCurrentWeek() {
		LocalDate now = new LocalDate();
		LocalDate sunday = now.withDayOfWeek(DateTimeConstants.SUNDAY);
		return sunday;
	}
	
	public static LocalDate getDateOfMondayByLocalDate(LocalDate date) {
		LocalDate dateMonday = date.withDayOfWeek(DateTimeConstants.MONDAY);
		return dateMonday;
	}
	
	public static Timestamp getDateOfMondayByYearAndWeek(int year, int week) {
		LocalDate dt = new LocalDate();
		dt = dt.withYear(year).withWeekOfWeekyear(week).withDayOfWeek(DateTimeConstants.MONDAY);
		return convertLocalDateToTimestamp(dt);
	}

	public static boolean isThisYear(java.util.Calendar calendar, Date date) {
		calendar.setTime(new Date());
		int thisYear = calendar.get(java.util.Calendar.YEAR);
		calendar.setTime(date);
		return calendar.get(java.util.Calendar.YEAR) == thisYear;
	}

	public static boolean isThisMonth(java.util.Calendar calendar, Date date) {
		calendar.setTime(new Date());
		int thisMonth = calendar.get(java.util.Calendar.MONTH);
		calendar.setTime(date);
		return calendar.get(java.util.Calendar.MONTH) == thisMonth;
	}
}
